<script setup lang="ts" name="registration">
import {reactive} from 'vue';
import {post} from "@/net";
import {ElMessage} from 'element-plus'
import router from "@/router";

const registrationForm = reactive({
  email: "",
  captcha: "",
  password: "",
  password_confirmation: ""
});


const getCaptcha = () => {
  post('/api/auth/valid-email', {
        email: registrationForm.email,
      },
      (message: any) => {
        ElMessage.success(message);
      },
      (message: any) => {
        ElMessage.warning(message);
      },
      (message: any) => {
        ElMessage.error(message);
      }
  )
}

const register = () => {
  post('/api/auth/register', {
        email: registrationForm.email,
        captcha: registrationForm.captcha,
        password: registrationForm.password
      },(message: any) => {
        ElMessage.success(message);
      },
      (message: any) => {
        ElMessage.warning(message);
      },
      (message: any) => {
        ElMessage.error(message);
      }
  )
}

</script>

<template>
  <div class="registration">
    <div class="registration-h3">
      <h3>注册账号</h3>
    </div>
    <div class="registration-form">
      <el-form label-position="left"
               label-width="auto"
               size="large"
      >
        <el-form-item label="邮箱" prop="email" >
          <el-input v-model="registrationForm.email" placeholder="请输入邮箱" id="email_register"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-input v-model="registrationForm.captcha" placeholder="请输入验证码"></el-input>
          <el-button @click="getCaptcha" id="captcha_button"> 获取验证码</el-button>
        </el-form-item>
        <el-form-item label="密码" prop="password" >
          <el-input type="password" v-model="registrationForm.password" placeholder="请输入密码" id="password_register"></el-input>
        </el-form-item>
        <el-form-item label="确定密码" prop="confirmPassword" >
          <el-input type="password" v-model="registrationForm.password_confirmation"
                    placeholder="请再次输入密码" id="password_register"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-button" type="primary" @click="register" id="register_button">注册</el-button>
          <el-button class="login-button" type="primary" @click="router.push('/login')" id="return_button"> 返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.registration {
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  overflow: hidden;
  width: 40vw;
  max-height: 60vh;
  min-height: 50vh;
  max-width: 100%;
  margin-top: 100px;
  backdrop-filter: blur(8px);
}

.registration-h3 {
  margin: auto;

}

.registration-form {
  margin: auto;
}

.login-button {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  height: 100%;
  margin: auto;
}
</style>